<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * WorkflowNodeUseror: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\workflow\logic;

use think\Db;
use think\Request;

/**
 * 流程节点关联用户=》逻辑层
 */
class WorkflowNodeUser extends WorkflowBase
{
    /**
     * 流程节点关联用户列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getWorkflowNodeUserList($where = [], $field = true, $order = '', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelWorkflowNodeUser->getList($where, $field, $order, $paginate)->toArray();
        if($paginate===false) $list['data']=$list;
        foreach ($list['data'] as &$row){
            $row['create_user_name']=$this->modelSysUser->getValue(['id'=>$row['create_user_id']],'realname');
            $row['destribute_type_name']=$this->modelWorkflowNodeUser->destribute_type($row['destribute_type'])['name'];
        }
        return $list;
    }

    /**
     * 流程节点关联用户添加
     * @param array $data
     * @return array
     */
    public function workflowNodeUserAdd($data = [])
    {
        $adddata=[];
        if(!empty($data['workflow_node_id'])){
           $rtn = $this->modelWorkflowNodeUser->deleteInfo(['workflow_node_id'=>$data['workflow_node_id']],true);
        }
        $destype=$this->logicWorkflowNode->getDestributeType($data['destribute_type']);
        if(!empty($destype['link'])){
            if(!empty($data['destribute_user_id'])){
                $destribute_user_arr=str2arr($data['destribute_user_id']);
                $destribute_user_name=str2arr($data['destribute_user_name']);
                foreach ($destribute_user_arr as  $key=>$uid){
                    $adddata[]=[
                        'workflow_node_id'=>$data['workflow_node_id'],
                        'deal_user_id'=>$uid,
                        'deal_user_name'=>$destribute_user_name[$key],
                        'create_user_id'=>SYS_USER_ID,
                    ];
                }
            }
        }
        $result = $this->modelWorkflowNodeUser->setList($adddata);

        $result && action_log('新增', '新增WorkflowNodeUserNode：' . $data['workflow_node_id']);
        $url = url('show');
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelWorkflowNodeUser->getError()];
    }

    /**
     * 流程节点关联用户删除
     * @param array $where
     * @return array
     */
    public function workflowNodeUserDel($where = [])
    {

        $result = $this->modelWorkflowNodeUser->deleteInfo($where,true);
        $result && action_log('删除', '删除WorkflowNodeUserNode，where：' . http_build_query($where));
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelWorkflowNodeUser->getError()];
    }

    /**流程节点关联用户信息
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getWorkflowNodeUserInfo($where = [], $field = true)
    {

        return $this->modelWorkflowNodeUser->getInfo($where, $field);
    }

    //分配类型
    public function  getDestributeType($key=null){
        return $this->modelWorkflowNodeUser->destribute_type($key);
    }

    public function  getLevel(){
        return $this->modelWorkflowNodeUser->level();
    }

    /**
     * 获取列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];
        //关键字查
        !empty($data['keywords']) && $where['name|content'] = ['like', '%' . $data['keywords'] . '%'];
        !empty($data['workflow_id']) && $where['workflow_id'] = ['=',  $data['workflow_id']];

        //下次联系时间
        if (!empty($data['next_rangedate'])) {
            $range_date = str2arr($data['next_rangedate'], "-");
            $where['next_time'] = ['between', $range_date];
        }
            //print_r($where);exit;

        return $where;
    }

    /**
     * 获取排序条件
     */
    public function getOrderBy($data = [])
    {
        $order_by = "";
        if (!empty($data['orderField'])) {
            $orderField = $data['orderField'];
            $orderDirection = $data['orderDirection'];
        } else {
            $orderField = "";
            $orderDirection = "";
        }
        if ($orderField == 'by_link') {
            $order_by = "link_time $orderDirection";
        } else if ($orderField == 'by_next') {
            $order_by = "next_time $orderDirection";
        } else if ($orderField == 'by_nodays') {
            $order_by = "nodays $orderDirection";
        } else {
            $order_by = "sort desc";
        }
        return $order_by;
    }

}
